﻿package editorPackage {
	
	import flash.text.*;
	import flash.events.MouseEvent;
	import flash.display.Sprite;
	import flash.text.Font;
	import flash.events.Event;
	
	public class DropDownList extends Sprite {
		private var elements:Array;
		private var expanded:Boolean;
		private var options:Array;
		private var selectedElement:TextField;
		private var optionIndex:int;
		private var activeElements:Array;
		private var w:int;
		
		public function DropDownList(e:Array, X:int, Y:int, W:Number = 180) {
			elements = e;
			x = X; 
			y = Y;
			w = W;
			drawBox(W, 20);
			
			activeElements = new Array(10);
			
			var format:TextFormat = new TextFormat("Arial",12);
			
			options = new Array(elements.length);
			for (var i:int = 0; i < elements.length; i++) {
				var o:TextField = new TextField();
				o.x = 0;
				o.y = 0;
				o.width = 160;
				o.height = 20;
				o.setTextFormat(format);
				o.wordWrap = false;
				o.multiline = false;
				o.type = TextFieldType.DYNAMIC; 
				o.textColor = 0xFFFFFF; 
				o.text = String(elements[i]);
				o.addEventListener(MouseEvent.CLICK, dropDownClick);
				o.autoSize = TextFieldAutoSize.NONE;
				options[i] = o;
			}
			addChild(options[0]);
			selectedElement = options[0];
			optionIndex = 0;
			activeElements.push(selectedElement);
			
			addEventListener(MouseEvent.MOUSE_WHEEL, scrollListener);
		}
		
		private function dropDownClick(e:MouseEvent) {
			if (expanded) {
				minimize(TextField(e.currentTarget));
			} else {
				expand();
			}
		}
		
		private function minimize(o:TextField) {
			drawBox(w, 20);
			o.x = 0;
			o.y = 0;
			selectedElement = o;
			optionIndex = options.indexOf(o);
			expanded = false;
			for (var i:int = 0; i < activeElements.length; i++) {
				var child = activeElements.pop();
				if (child != null && contains(child)) removeChild(child);
			}
			addChild(selectedElement);
			activeElements.push(selectedElement);
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		private function expand() {
			drawBox(w, 200);
			expanded = true;
			if (optionIndex > options.length - 10) optionIndex = options.length - 10;
			for (var i:int = 1; i < 10; i++) {
				var o:TextField = options[i+optionIndex]
				o.x = 0; o.y = i * 20;
				addChild(options[i+optionIndex]);
				activeElements.push(options[i+optionIndex]);
			}
		}
		
		public function setElement(str:String) {
			var i:int = 0;
			while (TextField(options[i]).text != str) {
				i++;
				if (i >= options.length) {
					i = -1;
					break;
				}
			}
			if (i > 0) {
				optionIndex = i;
				selectedElement = TextField(options[optionIndex]);
				minimize(selectedElement);
			}
		}
		
		private function drawBox(w:int, h:int, top:Boolean = false) {
			graphics.clear();
			graphics.beginFill(0x444444);
			graphics.lineStyle(1,0xAAAAAA);
			graphics.drawRect(0,0,w,h);
			graphics.endFill();
			
			graphics.beginFill(0xAAAAAA);
			graphics.lineStyle(1,0x555555);
			graphics.drawRect(0,0,w,20);
			graphics.endFill();
			
			graphics.beginFill(0x999999);
			graphics.lineStyle(0,0x666666);
			graphics.drawRect(w-20,0,20,h);
			graphics.endFill();
			
			if (h == 20) {
				graphics.beginFill(0x444444);
				graphics.lineStyle(1,0xAAAAAA);
				graphics.moveTo(w-16,4);
				graphics.lineTo(w-10,16);
				graphics.lineTo(w-4,4);
				graphics.lineTo(w-16,4);
				graphics.endFill();
			} else {
				
				graphics.beginFill(0x444444);
				graphics.lineStyle(1,0xAAAAAA);
				graphics.moveTo(w-16,h-16);
				graphics.lineTo(w-10,h-4);
				graphics.lineTo(w-4,h-16);
				graphics.lineTo(w-16,h-16);
				graphics.endFill();
				
				graphics.beginFill(0x444444);
				graphics.lineStyle(1,0xAAAAAA);
				graphics.moveTo(w-10,4);
				graphics.lineTo(w-16,16);
				graphics.lineTo(w-4,16);
				graphics.lineTo(w-10,4);
				graphics.endFill();
			}
		}
		
		private function scrollListener(e:MouseEvent) {
			if (expanded) {
				optionIndex -= e.delta;
				if (optionIndex < 0) optionIndex = 0;
				if (optionIndex > options.length - 10) optionIndex = options.length - 10;
				minimize(options[optionIndex]);
				expand();
			}
		}
		
		public function getSelectedValue():String { return selectedElement.text; }
	}
}